<?php
/**
 * UserController.php
 *
 * 用户控制器
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Home
 * @package		Home_Controller
 * @author 		Leo
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Momoger_Controller_Public */
//require_once 'Momoger/Controller/Public.php';

/**
 * 用户控制器
 *
 * @category	Home
 * @package		Home_Controller
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class UserController extends Momoger_Controller_Public {

	/**
	 * 控制器初始化
	 *
	 * @return void
	 */
	public function mmInit() {
		if (!$this->user) {
			$this->redirectWithMsg(3, 'You have not login, Please login first!', NULL, '/login');
			return ;
		}
	}

	/**
	 * 显示用户首页
	 *
	 * @return HTML
	 */
	public function indexAction() {
		//$this->logger->debug('start action');
		//
		//Zend_Debug::dump($this->user);
	}

	/**
	 * 用户控制中心列表接口
	 *
	 * @return HTML
	 */
	public function sliderAction() {
		// close layout
		if (null !== ($layout = Zend_Layout::getMvcInstance())) {
            $layout->disableLayout();
        }

		//set user valid categories to view
		$category = new Momoger_Model_Category();
		$rows = $category->getCategories($this->user->userID);
		$categories = array();
		foreach ($rows as $row) {
			if ($row['categoryActived']) {
				$categories[$row['categoryType']][] = $row;
			}
		}
		$this->view->categories = $categories;
	}

	/**
	 * 展示用户信息接口
	 *
	 * @return HTML
	 */
	public function infoAction() {
		$this->view->user = $this->user;
	}

	/**
	 * 用户修改昵称接口,
	 * 表单 id 为 nick-form
	 * 呢称 input id:nick-input. name:nick
	 *
	 * @return JSON|HTML
	 */
	public function nickAction() {

		//close view layout
		if (null !== ($layout = Zend_Layout::getMvcInstance())) {
            $layout->disableLayout();
        }

        //create form
		$form = new Momoger_Form_Nick();
		$nickForm = $form->getNickForm($this->getHelper('Url')->url(array('action' => 'nick', 'controller' => 'user', 'module' => 'home'), NULL, TRUE), $this->user->userNick);

		//check is post request or get request
		if(!$this->getRequest()->isPost()) {
			//sleep(1);
			//just view the html content
			$this->view->nickForm = $nickForm;
		} else {	//save user post data
			//setting default return data
			$jsonRs = array('result' => false, 'msg' => '');

			//user past data is valid
			if ($nickForm->isValid($_POST)) {
				//save user nick
				$this->user->userNick = $nickForm->getValue('nick');
				$user = new Momoger_Model_User($this->user);
				$user->save();

				//update reponse data
				$jsonRs['result'] = TRUE;
				$jsonRs['msg'] = 'Congratulations! Now, you nick is : ' . $this->user->userNick;
			} else {	// invalid post data
				$jsonRs['msg'] = $form->getErrorMsgs($nickForm->getErrors());

				//log it
				$this->logger->debug('AJAX rename user nick false!');
			}

			//reponse data to request client
			$this->getHelper('Json')->sendJson($jsonRs);
		}
	}

	/**
	 * 用户修改密码接口
	 * 表单 id 为 password-form
	 * 现用密码 input id:passwordold-input. name:passwdold
	 * 新密码 input id:passwordnew-input. name:passwdnew
	 *
	 * @return JSON|HTML
	 */
	public function passwdAction() {

		//close view layout
		if (null !== ($layout = Zend_Layout::getMvcInstance())) {
            $layout->disableLayout();
        }

        //create form
		$form = new Momoger_Form_Password();
		$passwdForm = $form->getPasswordForm($this->getHelper('Url')->url(array('action' => 'passwd', 'controller' => 'user', 'module' => 'home'), NULL, TRUE));

		//check is post request or get request
		if(!$this->getRequest()->isPost()) {
			//sleep(1);
			//just view the html content
			$this->view->passwdForm = $passwdForm;
		} else {	//save user post data
			//setting default return data
			$jsonRs = array('result' => false, 'msg' => '');

			//user past data is valid
			if ($passwdForm->isValid($_POST)) {
				//validate now password same or not
				if (md5($passwdForm->getValue('passwdold')) != $this->user->userPasswd) {
					$jsonRs['msg'] = 'Sorry! you old password is incorrect!';

					//log it
					$this->logger->debug('AJAX change user password false! old password invalid!');
				} else {
					//save user new password
					$this->user->userPasswd = md5($passwdForm->getValue('passwdnew'));
					$user = new Momoger_Model_User($this->user);
					$user->save();

					//update reponse data
					$jsonRs['result'] = TRUE;
					$jsonRs['msg'] = 'Congratulations! Now, you password was changed success!';
				}
			} else {	// invalid post data
				$jsonRs['msg'] = $form->getErrorMsgs($passwdForm->getErrors());

				//log it
				$this->logger->debug('AJAX change user password false!');
			}

			//reponse data to request client
			$this->getHelper('Json')->sendJson($jsonRs);
		}
	}

	/**
	 * 飞信发送接口
	 *
	 * @return HTML|JSON
	 */
	public function fetionAction() {
		//close view layout
		if (null !== ($layout = Zend_Layout::getMvcInstance())) {
            $layout->disableLayout();
        }

		//create form
		$form = new Momoger_Form_Fetion();
		$fetionForm = $form->getFetionForm($this->getHelper('Url')->url(array('action' => 'fetion', 'controller' => 'user', 'module' => 'home'), NULL, TRUE));

		//check is post request or get request
		if(!$this->getRequest()->isPost()) {
			//sleep(1);
			//just view the html content
			$this->view->fetionForm = $fetionForm;
		} else {	//save user post data
			//setting default return data
			$jsonRs = array('result' => false, 'msg' => '');

			//user past data is valid
			if ($fetionForm->isValid($_POST)) {
				//save user nick
				$inputs = $fetionForm->getValues();

				//get fetion object
				$fetion = new App_Util_Fetion($inputs['sender'], $inputs['passwd']);

				//send message
				if (!$fetion->sendMsg($inputs['receiver'], $inputs['sms'])) {
					$jsonRs['msg'] = $fetion->getErrorMsg();
				} else {
					$jsonRs['result'] = TRUE;
					$jsonRs['msg'] = 'Congratulations! Now, Message has been send!';
				}
			} else {	// invalid post data
				$jsonRs['msg'] = $form->getErrorMsgs($fetionForm->getErrors());

				//log it
				$this->logger->debug('AJAX send feiton false!');
			}

			//reponse data to request client
			$this->getHelper('Json')->sendJson($jsonRs);
		}
	}
}